if (evaluates over different Variables/Conditions)
if (name == "John") { print("Name is John") }
else if (name == "Bill") { print("Name is Bill") }
else if (name == "Lucy") { print("Name is Lucy") }
else { print("No match") }
switch (evaluates over the same Variable/Condition)
switch (name) {
case "John":
print("Name is John") //Only selected case is executed.
case "Bill":
print("Name is Bill") //There can be multiple commands inside each case
print("Found Bill")
default:
print("No match")
}
CONDITION ? TRUE : FALSE (simplified if...else)
//CONDITION ? TRUE : FALSE
name == "John" ? print("Name is John") : print("Name is NOT John") //All in a single line
for
for i in 1...5 { print(i) } //ITERATE THROUGH SEQUENCE: 1 2 3 4 5
for person in ["John", "Bill", "Lucy"] { print(person) } //ITERATE THROUGH ARRAY.
while
var i = 0
while(i < 5) {
i = i + 1
print(i)
}
repeat
var i = 0
repeat {
i = i + 1
print(i)
} while(i < 5)